package com.example.demo.controller;

import com.example.demo.entity.Role;
import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import com.example.demo.utils.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@CrossOrigin
@RestController

public class UserController {
    @Resource
    private UserService us;



    /**
     * 注册
     * */
    @ResponseBody
    @PostMapping(value = "register")
    public R reg(String user_name, String user_password, String user_email,String user_code){
        int j = us.reg(user_name, user_password, user_email,user_code);
        if (j > 0) {
            User user = us.userid(user_email);
            int k = us.role(user.getUserId());
            if (k > 0) {
                return ReSet.Ok(k);
            }else{
                return ReSet.Err();
            }
        }
        return ReSet.Err();
    }

    //发送邮件
    @PostMapping(value = "emails")
    public R emiles(String user_email) {
        User u = us.userid(user_email);
        if (u == null) {
            VerCodeGenerateUtil ver = new VerCodeGenerateUtil();
            String user_code = ver.generateVerCode();
            System.out.println(user_code);
            int i = us.emiles(user_email,user_code);
            if (i > 0){
                return ReSet.Ok(i);
            }else{
                return ReSet.Err();
            }
        }else {
            return ReSet.Err("该邮箱已被注册");
        }
    }

    //前端输入框调用，使用焦点得失调用此接口。
    @PostMapping(value = "select_email")
    public R select_email(String user_email) throws Exception{
        User user = us.select_email(user_email);
        if (user != null){
            return ReSet.Err("该邮箱已被注册");
        }
        return ReSet.Ok("该邮箱可用");
    }
    /**
     * 登录
     * */
    @ResponseBody
    @PostMapping(value = "login")
    public R log(String user_email, String user_password) throws Exception {
            User user = us.log(user_email, user_password);
            if (user != null && user.getUserStatus() == 1) {
                IO io = new IO();
                String token = JwtUtil.createToken(String.valueOf(user.getUserEmail()),user.getUserName());
                io.setToKenVer(token);
                io.setUser(user);
                return ReSet.Ok(io);
            }
            return ReSet.Err();
        }
    /**
     * 查看个人信息
     * */
    @PostMapping(value = "toview")
    public R view(String user_email){
        User user = us.toview(user_email);
        if (user != null){
            return ReSet.Ok(user);
        }
        return ReSet.Err();
    }
    /**
     * 查询所有学生
     * */
    @GetMapping("selstu")
    public R selstu(Integer page,Integer limit){
        List list = us.selstu(page, limit);
        if (list != null){
            return ReSet.Ok(list);
        }

        return ReSet.Err("加载失败，请重试!");
    }
    @PostMapping("selusernums")
    public R selusernums() {
        return ReSet.Ok(us.selusernums());
    }
    /**
     * 更改用户状态
     * */
    @PostMapping(value = "updaustatus")
    public R updaustatus(Integer user_status,String user_email){
        int i = us.update(user_status,user_email);
        if (i > 0){
            return ReSet.Ok(i);
        }
        return ReSet.Err();
    }
    /**
     * 用户自行更改密码
     * */
    @PostMapping("updatepassword")
    public R updatepassword(String new_user_password,String user_password,String user_email){
        int i = us.uppwd(new_user_password,user_password,user_email);
        if (i > 0){
            return ReSet.Ok(i);
        }
        return ReSet.Err();
    }
    /**
     * 管理员重置用户密码
     * */
    @PostMapping("admuppwd")
    public R admuppwd(String user_email){
        int i = us.admuppwd(user_email);
        if (i > 0){
            return ReSet.Ok(i);
        }
        return ReSet.Err();
    }
    /**
     * 查询角色
     * */
    @GetMapping("selrole")
    public R selrole() {
        List<Role> list =  us.selrole();
        if (list != null){
            return ReSet.Ok(list);
        }
        return ReSet.Err();
    }
    /**
     * 查询用户角色
     * */
    @GetMapping(value = "seluserrole")
    public R seluserrole(String user_email){
        Role role = us.seluserrole(user_email);
        if (role != null){
            return ReSet.Ok(role);
        }
        return ReSet.Err();
    }

    /**
     * 模糊查询用户
     */
    @PostMapping("selusermohu")
    public R selusermohu(String user_name, String user_email) {
        List users = us.selusermohu(user_name, user_email);
//        List list = new ArrayList();
//        for (int i = 0;i < users.size();i++){
//            List list1 = users.get(i);
//        }

        return ReSet.Ok(users);
    }
}
